<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Email;
use think\Response;
use think\Session;
use think\Db;
use think\Exception;

class Order extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*'; 
    
    public function index()
    { 
        $res =  Db::table('fa_order')->find($this->request->get('id'));
        // var_dump($res);
        $this->view->assign('res', $res);


        if($res['big_number'] == 'PZAH202042010000000051(10w)' || $res['big_number'] == 'PZAH202042010000000051(15w)' || $res['big_number'] == 'PZAH202042010000000051(20w)' || $res['big_number'] == 'PZAH202042010000000051(30w)' || $res['big_number'] == 'PZAH202042010000000051(40w)'){
            return $this->view->fetch('order/pzah');
        }elseif($res['big_number'] == 'PZAH202042010000000051(YX15w)'){
            return $this->view->fetch('order/pzah-yx');
        }elseif($res['big_number'] == 'PZAH201942010000000077(TQ50w)'){
            return $this->view->fetch('order/pzah-tq');
        }elseif($res['big_number'] == 'PZAH202042010000000051(NEW 20W)' || $res['big_number'] =='PZAH202042010000000051(NEW 40W)' || $res['big_number'] == 'PZAH202042010000000074(20W)' || $res['big_number'] =='PZAH202042010000000074(50W)'){
            return $this->view->fetch('order/new');
        }elseif($res['big_number'] == 'PZAH202042010000000074(10W)' || $res['big_number'] =='PZAH202042010000000051(NEW 10W)'){
            return $this->view->fetch('order/202001229new');
        }else{ 
            return $this->view->fetch();
        } 
    }
     
     public function nullify($ids = NULL){    
        Db::startTrans();
        try {
        	if(Session::get('userName')){
                $rt = Db::table('fa_order')->where('id', $ids)->find();
                if($rt['nullify'] == 1){ 
                    $this->error('该保单已作废，请勿重复操作！'); 
                    exit;
                }
        		//修改
	            $result = Db::table('fa_order')->where('id', $ids)->update(['nullify' => 1]);
	            $data = array();
	            $data['userName'] = Session::get('userName');
	            $data['type'] = '作废';
	            $data['create_time'] = time();
	            $data['remark'] = '作废保单：'.$ids; 
	            Db::table('fa_order_log')->insert($data);  
	            //用户保单余额+1 
	            $result = Db::table('fa_order')->where('id', $ids)->find();

                if($result['insurance_company'] == 'PICC'){
                     $this->sendEmail($result['id'],$result['big_number'],$result['small_number'],$result['real_name'],'保单作废',$result['email']);
                } 

	            $res = Db::table('fa_warranty')->where('userName',$result['userName'])->where('type',$result['big_number'])->setInc('num',1);
	            if($res){
	                Db::commit();
     	 			$this->success('操作成功');
	            }else{ 
	                Db::rollback();
	                $this->error($e->getMessage()); 
	            }
        	} 
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage()); 
        }
    } 
    //发送邮箱
    private function sendEmail($id,$big_number,$small_number,$name,$type,$email_address){
        \think\Config::set('site', array_merge(\think\Config::get('site'), [])); 
        $email = new Email;
        $html = file_get_contents("http://wx.350gps.cn/index/order/index.html?id={$id}");
        $result = $email
            ->to($email_address)
            ->subject("大保单{$big_number}子保单{$small_number}客户名字{$name}【{$type}】")
            ->message($html)
            ->send(); 
    }

}
